<template>
  <div class="custom-body" :style="borderStyle">
    <el-empty
      v-if="dataList === null || dataList.length === 0"
      class="custom-right"
      :image-size="100"
    />
    <div v-else class="custom-right">
      <div
        v-for="(item, index) in dataList"
        :key="'li-' + index"
        class="custom-ul-li"
        :style="liStyle"
      >
        <div
          class="custom-ul-body"
          :style="{
            boxShadow: '0 2px 7px 0px ' + item.backColorA,
            background:
              'linear-gradient(0deg,' +
              item.backColorA +
              ',' +
              item.backColorB +
              ')'
          }"
          @click="callBack(item)"
        >
          <div class="custom-li-image">
            <el-image :src="baseURL + item.tb.substring(1, item.tb.length)" />
          </div>
          <div class="custom-li-right">
            <div class="custom-li-line">
              <span
                :style="{
                  color: item.fontColor,
                  fontSize: item.fontSize + 'px',
                  fontWeight: '600'
                }"
              >
                <statistics-slot :ids="item.sjly.id" :column="item.number" />
              </span>
              <span
                :style="{
                  color: item.titleColor,
                  fontSize: item.titleFontSize + 'px'
                }"
              >{{ item.label }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import StatisticsSlot from './slot/statistics';
import { queryEntity } from '@/api/portal/manager/columnTemp';
export default {
  name: 'PortalNewPcStatisticsV1',
  components: { StatisticsSlot },
  props: {
    temp: {
      type: Object,
      default() {
        return {};
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      column: [],
      dataList: [],
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      borderStyle: {}
    };
  },
  computed: {
    themes() {
      return this.$store.getters.theme;
    },
    liStyle() {
      const width = (100 / this.column.size).toFixed(2);
      const height = (100 / this.column.rows).toFixed(2);
      return { width: width + '%', height: height + '%' };
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx);
          this.column = pzx;
          this.dataList = pzx.data;
        } else {
          const pzx = JSON.parse(newData.pzx);
          this.column = pzx;
          this.dataList = pzx.dataList;
          // this.initData(newData.id)
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    themes: {
      handler(val, oldVal) {
        this.borderStyle.borderRadius = val.bkdx;
        if (this.temp.sfxsbk === true) {
          this.borderStyle.border = '1px solid ' + val.bkys;
        }
        if (this.temp.bkyy === true) {
          this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)';
        }
        this.borderStyle.background = val.columnGroundColor;
        this.borderStyle.color = val.fontColor;
        this.borderStyle = Object.assign({}, this.borderStyle);
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initEntity(id, label) {
      if (id !== null) {
        queryEntity({ id: id }).then(res => {
          const { data } = res;
          this.call = data;
          this.call.menuId = this.temp.menu.id;
          this.call.menuName = this.temp.menu.name;
          this.call.showHeader = true;
          this.call.columnName = label;
          this.$emit('fetch-data', this.call);
        });
      }
    },
    callBack(item) {
      if (item.isOpenMore === true) {
        if (item.openType === 1) {
          // 模版的方式打开
          this.initEntity(item.temp, item.label);
        }
        if (
          item.openType === 2 &&
          undefined !== item.openUrl &&
          item.openUrl !== null
        ) {
          window.open(item.openUrl, '_blank');
        }
      }
      /* if (undefined !== item) {
        item.menuId = this.temp.menu.id
        item.menuName = this.temp.menu.name
        item.showHeader = true
        this.$emit('fetch-data', item)
      } */
    }
  }
};
</script>

<style scoped>
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-flow: row nowrap;
  border-radius: 4px;
  box-sizing: border-box;
  overflow: hidden;
}
.custom-right {
  flex: 1 1 auto;
  height: 100%;
  display: flex;
  flex-flow: row wrap;
}
.custom-ul-li {
  flex: 0 1 auto;
  padding: 15px;
  box-sizing: border-box;
  height: 100%;
}
.custom-ul-body {
  width: 100%;
  height: 100%;
  border-radius: 4px;
  position: relative;
  box-sizing: border-box;
}
.custom-li-image {
  width: 40px;
  height: 40px;
  position: absolute;
  top: 50%;
  margin-top: -25px;
  left: 15px;
}
.custom-li-right {
  flex: 1 1 auto;
  height: 100%;
  padding-left: 55px;
  box-sizing: border-box;
  position: relative;
}
.custom-li-line {
  position: absolute;
  left: 0px;
  right: 0px;
  height: 50px;
  top: 50%;
  margin-top: -25px;
  text-align: center;
}
.custom-li-line span {
  width: 100%;
  height: 25px;
  line-height: 25px;
  text-align: center;
}
</style>
